package dao.mapMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IEventType;
import dao.DAOException;
import dao.IEventTypeDAO;


/**
 *
 * @author salvador
 */
public class EventTypeMapMySqlDAO implements IEventTypeDAO {

    public void create(IEventType eventType) throws DAOException {
        MapMySqlDBConnection.getInstance().executeUpdate("INSERT INTO event_type(name) VALUES('"+eventType.getName()+"')");
    }

    public void delete(IEventType eventType) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(IEventType eventType) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public IEventType retrieve(int id) throws DAOException {
        ResultSet rs = MapMySqlDBConnection.getInstance().executeQuery("SELECT * FROM event_type WHERE id='"+id+"'");
        IEventType eventType = null;
        try
        {
            while (rs.next())
            {
                eventType = AbstractModelFactory.getModelFactory().getEventType();
                eventType.setId(rs.getInt("id"));
                eventType.setName(rs.getString("name"));
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
    	return eventType;
    }

    public List<IEventType> retrieveAll() throws DAOException
    {
        ResultSet rs = MapMySqlDBConnection.getInstance().executeQuery("SELECT * FROM event_type");
        List<IEventType> eventTypeList = new ArrayList<IEventType>();
        try
        {
            while (rs.next())
            {
                IEventType eventType = AbstractModelFactory.getModelFactory().getEventType();
                eventType.setId(rs.getInt("id"));
                eventType.setName(rs.getString("name"));
                eventTypeList.add(eventType);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
    	return eventTypeList;
    }

}
